<template>
  <div class="app-container">
    <el-table
      :height="tableHeight"
      header-cell-class-name="aa"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        align="center"
        type="index"
        width="50"
      />
      <el-table-column
        prop="title"
        align="center"
        label="名称"
      />
      <el-table-column
        prop="categoryName"
        align="center"
        label="关联分类"
      />
      <el-table-column
        prop="categoryName"
        align="center"
        label="标题图片"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.titleImg"
            fit="fit"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="categoryName"
        align="center"
        label="图片"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.imgPath"
            fit="fit"
          />
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openUpdateHomeImg(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">

        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="title" />
        </el-form-item>

        <el-form-item label="标题图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="upLoadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccessTitle"
          >
            <img v-if="imageUrl" :src="imageUrlTitle" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="upLoadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="关联分类" :label-width="formLabelWidth">
          <el-select ref="selection" v-model="value" filterable placeholder="请选择" @change="changeType">
            <el-option
              v-for="item in options"
              :key="item.categoryType"
              :label="item.categoryName"
              :value="item.categoryType"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateHomeType()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, queryCategory, updateHomeType } from '@/api/homeType'
export default {
  name: 'Index',
  data() {
    return {
      upLoadUrl: process.env.VUE_APP_BASE_API + 'product-info/upload',
      title: '',
      id: '',
      isUpdate: false,
      tableHeight: window.innerHeight - 170,
      tableData: [],
      formLabelWidth: '100px',
      dialogFormVisible: false,
      imageUrl: '',
      imageUrlTitle: '',
      form: {
        region: '',
        name: ''
      },
      options: [],
      value: 1,
      categoryId: ''
    }
  },
  mounted() {
    this.getList()
    this.queryCategory()
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data
    },
    handleAvatarSuccessTitle(res, file) {
      this.imageUrlTitle = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    getList() {
      getList().then(response => {
        this.tableData = response.data
      })
    },
    openUpdateHomeImg(row) {
      this.dialogFormVisible = true
      this.imageUrl = row.imgPath
      this.imageUrlTitle = row.titleImg
      this.title = row.title
      this.categoryId = row.categoryId
      this.value = parseInt(row.categoryId)
      this.categoryName = row.categoryName
      this.id = row.id
    },
    updateHomeType() {
      const data = {
        id: this.id,
        categoryId: this.value,
        imgPath: this.imageUrl,
        titleImg: this.imageUrlTitle,
        title: this.title,
        categoryName: this.$refs.selection.selectedLabel

      }
      updateHomeType(data).then(response => {
        this.$message({
          type: 'success',
          message: '编辑成功!'
        })
        this.getList()
        this.dialogFormVisible = false
      })
    },
    queryCategory() {
      queryCategory().then(response => {
        this.options = response.data
      })
    },
    changeType(value, label) {
      console.log(label)
    }
  }
}
</script>

<style lang="scss" scoped>
  .btn-container{
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    margin-bottom: 10px;
  }
  .el-icon-plus .avatar-uploader-icon {
    border: 1px dashed #d9d9d9!important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader i{
    border: 1px dashed #d9d9d9!important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
